// pages/ordinary/ordinary.js
const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex:0,
    timeShow:false,
    initDate: '1995-01-01',
    showYear:true,
    birthdayDefault: '',
    birthdayTime:'',
    birthdayLunarStr:'',
    phone:'',
    blessings:'',
    blessingsMb: [],
    blessingsnum: 0,
    blessingsxg: false,
    name:'',
    userInfo:'',
    ifNotice:false,
  },
  sexbtn(){
    if(this.data.sex ==0){

    }else{
      this.setData({
        sex:0,
      })
    }
  },
  sexbtn1() {
    if (this.data.sex == 0) {
      this.setData({
        sex: 1,
      })
    } else {
    }
  },
  peoplenameInput(e){
    var that = this
    var peoplename = e.detail.value
    that.setData({
      name: peoplename
    })
  },
  phoneInput(e){
    var that = this
    that.setData({
      phone: e.detail.value.trim()
    })
  },
  phoneblur(e){
    var that = this
    that.setData({
      phone: e.detail.value
    })
    var isphone = /^((1[3|4|5|6|7|8|9][0-9]{9}))$/;
    if (that.data.phone.trim() != '' && (isphone.test(that.data.phone))){
    app.req({
      url: "blessmini/blesscommon/addBirthdayCheckUser",
      data: {
        tel: that.data.phone,
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          var id = res.data.actId
          if (res.data.actId) {
            wx.showModal({
              title: '提示',
              content: '此寿星已有生日活动啦，点击确认去送祝福',
              cancelText:'重新输入',
              cancelColor:'#888888',
              confirmText: '确认',
              confirmColor: '#576B95',
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/ordinaryDetails/ordinaryDetails?id=' + id,
                  })
                } else if (res.cancel) {
                  that.setData({
                    phone:'',
                  })
                }
              },
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }
  },
  //输入祝福语
  blessingInput: function (e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        blessings: ''
      })
    } else {
      this.setData({
        blessings: e.detail.value,
        blessingsxg: true,
      })
    }
  },
  bindTextAreaBlur(e){
    this.setData({
      blessings: e.detail.value.replace(/\s+/g, ''),
      ifNotice: false
    })
  },
  ifNotice() {
    this.setData({
      ifNotice: true
    })
  },
  blessingsbtn() {
    var that = this
    var blessingsnum = that.data.blessingsnum + 1
    if (blessingsnum == that.data.blessingsMb.length) {
      blessingsnum = 0
    }
    if (that.data.blessingsxg) {
      wx.showModal({
        content: '已编辑内容将会被替换，是否确认',
        success(res) {
          if (res.confirm) {
            that.setData({
              blessingsnum: blessingsnum,
              blessings: that.data.blessingsMb[blessingsnum].dictName,
              blessingsxg: false,
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      that.setData({
        blessingsnum: blessingsnum,
        blessings: that.data.blessingsMb[blessingsnum].dictName
      })
    }
  },
  timeShow(e) {
    this.setData({
      timeShow: true,
    })
  },
  submit: function (e) {
    console.log(e);
    console.log('日期类型：' + e.detail.dateType)
    console.log('是否显示年份：' + e.detail.showYear)
    console.log('日期字符串：' + e.detail.dateStr)
    if (e.detail.dateType == '公历') {
      this.setData({
        birthdayTime: e.detail.dateStr + ' ' + '00:00:00',
        birthdayDefault: 0,
        showYear: e.detail.showYear
      })
      console.log('公历判断', this.data.birthdayTime, this.data.initDate)
      console.log('时间', this.data.showYear)
    } else {
      console.log('农历', 11111)
      this.setData({
        birthdayLunarStr: e.detail.dateStr,
        birthdayDefault: 1
      })
    }
    if (this.data.birthdayDefault == 0) {
      this.setData({
        dateInfo: utils.itemDateToShow(this.data.birthdayTime, 1, false, false).substring(5, 11)
      })
      console.log(this.data.dateInfo)
    } else {
      this.setData({
        dateInfo: this.data.birthdayLunarStr.substring(9, 13)
      })
      console.log(this.data.dateInfo)
    }

  },
  save(){
    var that = this
    var phonePattern = /^((1[3|4|5|6|7|8|9][0-9]{9}))$/;
    if(that.data.name==''){
      wx.showToast({
        title: '请输入姓名',
        icon:'none'
      })
      return;
    } else if (that.data.dateInfo==''){
      wx.showToast({
        title: '请设置生日',
        icon: 'none'
      })
      return;
    } else if (that.data.phone ==''){
      wx.showToast({
        title: '请输入寿星手机号',
        icon: 'none'
      })
      return;
    } else if (!phonePattern.test(that.data.phone)){
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return;
    }else{
      app.req({
        url: "blessmini/blesscommon/addBirthdayCheckUser",
        data: {
          tel: that.data.phone,
        },
        method: "post",
        success: function (res) {
          if (res.code == 200) {
            if (res.data.actId){
              var id = res.data.actId
              wx.showModal({
                title: '提示',
                content: '此寿星已有生日活动啦，点击确认去送祝福',
                cancelText: '重新输入',
                cancelColor: '#888888',
                confirmText: '确认',
                confirmColor: '#576B95',
                success: function(res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: '/pages/ordinaryDetails/ordinaryDetails?id='+id,
                    })
                  } else if (res.cancel) {
                    that.setData({
                      phone: '',
                    })
                  }
                },
              })
            }else{
              that.addOrdinaryBirthday()
            }
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  addOrdinaryBirthday(){
    var that = this
    var time = ''
    if (that.data.showYear){
      time=that.data.birthdayTime
      console.log('时间1', time)
    }else{
      time = '1995-'+ that.data.birthdayTime
      console.log('时间2', time)
    }
    console.log('时间',time)
    app.req({
      url: "blessmini/blesscommon/addOrdinaryBirthday",
      data: {
        name: that.data.name,
        sex:that.data.sex,
        birthday:time,
        mobile:that.data.phone,
        description: that.data.blessings
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
            wx.redirectTo({
              url: '/pages/ordinaryDetails/ordinaryDetails?id=' + res.data,
            })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getblessings(){
    var that = this
    app.req({
      url: "/wechat/dictionary/listBydictType",
      data: {
        dictType: 'BLESSINGS',
      },
      method: "post",
      success: function (res) {
        that.setData({
          blessingsMb:res.data,
          blessingsnum: 0,
          blessings: res.data[0].dictName,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.setData({
        userInfo: userInfo
      })
    })
    that.getblessings()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})